<?php

namespace backend\modules\user\controllers;

use common\helpers\ArrayHelper;
use common\models\common\Provinces;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\user\Shoporder;
use common\enums\StatusEnum;
use backend\controllers\BaseController;
/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class ShoporderController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Shoporder::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {

        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'menuDropDownList' =>['0'=>'代发货','1'=>'已发货'],
        ]);
    }

    /**
     * ajax编辑/创建
     *
     * @return mixed|string|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->findModel($id);

        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
            $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
            $model->ordernumber=$orderSn;
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }

        return $this->renderAjax($this->action->id, [
            'model' => $model,
        ]);
    }

  public function  actionUp(){

        $id = Yii::$app->request->get('id');
        $type = Yii::$app->request->get('type');
        if($type){
           // $a = self::actionHu();
            return $this->message("发货失败,不能重复发货", $this->redirect(Yii::$app->request->referrer), 'error');
        }
        $data = Shoporder::findOne($id);
        $data->type=1;
       if ($data->save()){
            return $this->message("发货成功", $this->redirect(['index', 'cate_id' => $model->cate_id]));
       }

  }

}